<?php
  require_once("../includes/config.php"); 
  $length = 20;
  if( isset($_POST['page'])  && !empty($_POST['page'] ))
  {
    $page =  filter_var($_POST['page'], FILTER_SANITIZE_NUMBER_INT);
    $start = $page*20 - 20;
  }
  else
  {
    $start = 0;
    $page = 1;
  }
 
  if(isset($_POST['year']))
  {
    $month = $_POST['month'];
    $year = $_POST['year'];
  }
  else
  {
    $month = intval(date('m', time()) );
    $year = intval(date('Y', time()));
  }
  
  $sql="SELECT * FROM apartments
		WHERE apartments.owner_id=?";
		
  $sqlCount= "SELECT COUNT(*) FROM apartments
		WHERE apartments.owner_id=?";
   
   $binds = array();
   $binds['owner_id'] = $_SESSION['user_id'];
     
  if(!empty($_POST['unit_number']))
  {
    $binds['unit_number'] = $_POST['unit_number'] ;
    $sql .= " and apart_name=?";
    $sqlCount .= " and apart_name=?";
  }
  
  if(!empty($_POST['floor#']))
  {
    if(!empty($binds))
      $and = " and ";
    else
      $and = "  ";
          
    $binds['floor#'] = $_POST['floor#'];
    $sql .= $and." apartments.floor = ?";
    $sqlCount .= $and." apartments.floor = ?";
  }
  
  if(!empty($_POST['room_name']))
  {
    if(!empty($binds))
      $and = " and ";
    else
      $and = "  ";
          
    $binds['room_name'] = $_POST['room_name'];
    $sql .= $and." apart_name = ?";
    $sqlCount .= $and." apart_name = ?";
  }

  $sql .= " ORDER BY apart_name LIMIT ?,?";
  $binds['start'] = $start;
  $binds['end'] = $length;
  $aparts = query($sql,$binds);
	
  $marks = array();
  $j = 0;
  
  foreach($aparts as $apart)
  {
    $binds['month'] = $month;
    $binds['year'] = $year;
    $binds['month1'] = $month;
    $binds['year1'] = $year;

    
    $contracts = query("SELECT * FROM contract_apartments LEFT JOIN contracts ON contracts.contract_id = contract_apartments.contract_id LEFT JOIN companies ON contracts.company_id=companies.company_id WHERE apartment_id = ? 
    and (MONTH(actual_in) =? and year(actual_in) =? or (MONTH(created_at) = ? and year(created_at) =?)) ORDER BY actual_in", $apart['id'],$month,$year,$month,$year);
    
    $marks[$j] = array_fill(0, 32, 0);
    $i = 0;
    foreach($contracts as $contract)
    {
	  $i++;
      $timestamp = strtotime($contract['actual_in']);
      $day_in = intval(date('d', $timestamp));

	  //preparing for a scenario where creation date is in current queried month while actual in date in other month
	  $in = date("m",strtotime($contract['actual_in']));
	  if($in > $month)
	  {
		$max_day = cal_days_in_month ( CAL_GREGORIAN, $month, $year ); //return max number of date for a given month, Because I only care about current month
        $end_of_month =  $max_day;
		$created_at = date("d",strtotime($contract['created_at']));
		for ($i = $created_at; $i<=$end_of_month;$i++)
		{
			$marks[$j][$i] = 1;
		}

		continue;
	  }			  		

	  if($contract['actual_ot'] == 0)
		$timestamp1 = strtotime($contract['miladi_out']);
	  else	
		$timestamp1 = strtotime($contract['actual_ot']);
		
      $day_out = intval(date('d', $timestamp1));
      if( $day_out < $day_in ) //$day_out extends to other month
      {
        $max_day = cal_days_in_month ( CAL_GREGORIAN, $month, $year ); //return max number of date for a given month, Because I only care about current month
        $day_out =  $max_day;
      }
      
      for( $k = $day_in; $k <=$day_out;$k++ )
      {
        $marks[$j][$k] = 2;
      }   

      if($contract['created_at'] == 0)
        $created_at = -1;
        
      else
      {
        $timestamp = strtotime($contract['created_at']);
        $created_at = intval(date('d', $timestamp));
      }
	  
	  
      if( $created_at <  $day_in && $created_at != -1 )
      {
        for($k = $created_at; $k <$day_in;$k++)
        {

          if($marks[$j][$k] != 2)
          {
            $marks[$j][$k] = 1;
          }
        }
        

      }
	  
	

      $created_at = -1;
    } 
    
    $j++;

  }
  $Count = query($sqlCount,$binds);
  $totalPages = $Count[0]['COUNT(*)'];
  
  render("units_reports.php", array("title"=>"تقرير حركة الوحدات","aparts"=>$aparts,"page"=>$page,"totalPages"=>$totalPages,"start"=>$start,"length"=>$length,"binds"=>$binds,"month"=>$month,"year"=>$year,'binds'=>$binds,"marks"=>$marks));
  
  ?>